package com.controller;

import com.api.CommonResult;
import com.entity.table.BTypeTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.BType;
import com.service.BTypeService;

import java.io.Serializable;
import java.util.List;

/**
 * 商品类型表 控制层。
 *
 * @author admin
 * @since 2024-05-24
 */
@Controller
@RequestMapping("/admin/type")
public class BTypeController {

    @Autowired
    private BTypeService bTypeService;

    @GetMapping(value = "/typeList")
    public String typeList(ModelMap model, @RequestParam(value = "typeName",defaultValue = "") String typeName,
                           @RequestParam(value = "p" ,defaultValue = "1") int p,
                           @RequestParam(value = "pageSize" ,defaultValue = "10") int pageSize){
        QueryWrapper queryWrapper = QueryWrapper.create().where(BTypeTableDef.B_TYPE.TYPE_NAME.like(typeName));
        Page<BType> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<BType> pageInfo = bTypeService.page(page,queryWrapper);
        model.addAttribute("page",pageInfo);
        model.addAttribute("total",pageInfo.getTotalRow());
        return "back/type-list";
    }
    @GetMapping(value = "/addView")
    public String addTypeView(){ return "back/type-add"; }
    /**
     * 添加商品类型表。
     *
     * @param bType 商品类型表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody BType bType) {
//        return bTypeService.save(bType);
//    }
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(BType bType) {
        try {
            boolean r = bTypeService.save(bType);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = bType.getTypeName()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping(value = "updateView")
    public String updateView(int id,ModelMap model){
        BType bType = bTypeService.getById(id);
        model.addAttribute("type",bType);
        return "back/type-edit";
    }

    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try {
            boolean r = bTypeService.removeById(id);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
    /**
     * 根据主键删除商品类型表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return bTypeService.removeById(id);
    }

    /**
     * 根据主键更新商品类型表。
     *
     * @param bType 商品类型表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(BType bType) {
        try {
            boolean r = bTypeService.updateById(bType);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = bType.getTypeName()+"已存在";
            return CommonResult.failed(msg);
        }
    }
//    @PutMapping("update")
//    public boolean update(@RequestBody BType bType) {
//        return bTypeService.updateById(bType);
//    }

    /**
     * 查询所有商品类型表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<BType> list() {
        return bTypeService.list();
    }

    /**
     * 根据商品类型表主键获取详细信息。
     *
     * @param id 商品类型表主键
     * @return 商品类型表详情
     */
    @GetMapping("getInfo/{id}")
    public BType getInfo(@PathVariable Serializable id) {
        return bTypeService.getById(id);
    }

    /**
     * 分页查询商品类型表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<BType> page(Page<BType> page) {
        return bTypeService.page(page);
    }

}
